package vn.mobiistar.videoapp.ui;

import java.util.ArrayList;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.other.Video;
import vn.mobiistar.videoapp.other.VideoAdapter;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/* @NamNguyenGiang 
 * Quan ly mot khoi block video
 * Mot khoi block bao gom1 TextView Title va 1 listview 
 */
public class BlockView extends LinearLayout {
	
	// mode, scroll to loadmore = true, button to loadmore
	private boolean mMode = true;
	
	// title of video's block
	private TextView mTitle;

	// list of video
	private ListView mList;

	// load more button
	private Button mLoadMore;

	// adapter for mList
	private VideoAdapter mAdapter;

	// data for adapter
	private ArrayList<Video> mData;

	// type of block. 0: test data, 1: new video, 2 mostview video, 3 video of category
	private int mType = 0;
	
	// context
	private Context mContext;

	private EmptyLoadingView mLoadingView;

	private TextView mMgs;

	// set block title
	public TextView getTitle() {
		return mTitle;
	}
	public void setType(int mType) {
		this.mType = mType;
	}
	public Button getLoadMore() {
		return mLoadMore;
	}
	// init function for block
	public BlockView(Context context, AttributeSet attr) {
		super(context, attr);
		LayoutInflater la = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		la.inflate(R.layout.block, this, true);

		mList 		= (ListView) this.findViewById(R.id.bl_listview);
		mMgs 		= (TextView) this.findViewById(R.id.bl_msg);
		mTitle 		= (TextView) this.findViewById(R.id.bl_blocktitle);
		mLoadMore 	= (Button) this.findViewById(R.id.bl_loadmore);
		mLoadingView= (EmptyLoadingView) this.findViewById(R.id.bl_loading);
		mContext = context;
		mData = new ArrayList<Video>();
	}
	//return loading view
	public EmptyLoadingView getLoadingView() {
		return mLoadingView;
	}
	// set adapter for displaying listview
	public void setAdapter() {
		mAdapter = new VideoAdapter(mContext, mData, mList);
		mList.setAdapter(mAdapter);
		if(mMode) resetListHeight();
		
	}
	// set adapter for displaying listview
	public void setData(ArrayList<Video> listVideo) {
		mData.clear();
		mData = listVideo;
		if(mData.size() >0) {
			mMgs.setVisibility(View.GONE);
		}else{
			mMgs.setVisibility(View.VISIBLE);
		}
		setAdapter();
	}
	
	// listview dont have scrollbar.
	private void resetListHeight(){
		int totalHeight = 0;//it is the ListView Height
		for (int i = 0, len = mAdapter.getCount(); i < len; i++) {
			
		    View listItem = mAdapter.getView(i, null, mList);
		    listItem.measure(0, 0);
		    int list_child_item_height = listItem.getMeasuredHeight()+mList.getDividerHeight();//item height
		    totalHeight += list_child_item_height; //
		}
		mList.getLayoutParams().height = totalHeight;
	}
	//return listview
	public ListView getList() {
		return mList;
	}
	
	// set block title
	public void setTitle(int textResId) {
		mTitle.setText(textResId);
	}
	public void setTitle(String text) {
		mTitle.setText(text);
	}
	
	// set no block title
	public void setNoTitle(){
		mTitle.setVisibility(View.GONE);
		this.findViewById(R.id.bl_emptyview).setVisibility(View.GONE);
	}
	
	// set no button loadmore
	public void setNoButtonLoadMore(){
		mLoadMore.setVisibility(View.INVISIBLE);
	}
	
	// set type. scroll or button loadmore
	public void switchToScrollMode(){
		mList.setVerticalScrollBarEnabled(true);
		setNoButtonLoadMore();
		setNoTitle();
		mMode = false;
	}
	
	//set msg
	public void setmMgs(String mMgs) {
		this.mMgs.setText(mMgs);
		this.mMgs.setVisibility(View.VISIBLE);
	}
	
	// an mesage
	public void unsetMsg(){
		this.mMgs.setVisibility(View.GONE);
	}
	
	// notifi data changed
	public void notifiDataSetChanged(){
		mAdapter.notifyDataSetChanged();
	}
}
